'use strict';

module.exports = app => {
    const {
        router,
        controller,
        io
    } = app;

    const {
        needs
    } = app.config;

    //开放接口
    router.get('/api', controller.index.index);
    router.get('/api/getToken', controller.index.getToken);
    router.get('/api/getTimestamp', controller.index.getTimestamp);
    //router.get('/api/getAreaInfo', controller.index.getAreaInfo);
    //获取加密密钥
    router.get('/api/getDHKey', controller.index.getDHKey);
    //文件上传相关接口
    router.post('/api/upload/uploadSingleFile', controller.uploadFile.uploadSingleFile);
    router.post('/api/upload/uploadMultipartFile', controller.uploadFile.uploadMultipartFile);
    router.post('/api/upload/uploadAudioFile', controller.uploadFile.uploadAudioFile);
    router.post('/api/upload/uploadVersion', controller.uploadFile.uploadVersion);

    router.post('/api/upload/uploadErrorLog', controller.uploadFile.uploadErrorLog);
    router.post('/api/upload/addFragFile', controller.uploadFile.addFragFile);
    router.get('/api/upload/getChunkFileList', controller.uploadFile.getChunkFileList);
    router.get('/api/upload/getLastFileIndex', controller.uploadFile.getLastFileIndex);
    router.get('/api/upload/getErrorLogInfo', controller.uploadFile.getErrorLogInfo);

    let noCreateRouter=['index','controllerBase','uploadFile'];//不创建的控制类，额外补充
    Object.keys(controller).forEach((keyName) => {
        let keyItem = controller[keyName];
        if (noCreateRouter.indexOf(keyName)===-1) {
            if (needs.dirNameList.indexOf(keyName) > -1) {
                Object.keys(keyItem).forEach(childKeyName => {
                    //childKeyName 为controller文件名称
                    let controllerObj = keyItem[childKeyName];
                    Object.keys(controllerObj).forEach(funName => {
                        let optName = funName.substring(0, 3).toLowerCase(); //获取方法
                        switch (optName) {
                            case "get":
                                router.get(`/api/${keyName}/${childKeyName}/${funName}`, controllerObj[funName]);
                                break;
                            default:
                                router.post(`/api/${keyName}/${childKeyName}/${funName}`, controllerObj[funName]);
                                break;
                        }
                    })
                })
            } else {
                //keyName 为controller文件名称
                Object.keys(keyItem).forEach(funName => {
                    let optName = funName.substring(0, 3).toLowerCase(); //获取方法
                    switch (optName) {
                        case "get":
                            router.get(`/api/${keyName}/${funName}`, keyItem[funName]);
                            break;
                        default:
                            router.post(`/api/${keyName}/${funName}`, keyItem[funName]);
                            break;
                    }
                })
            }
        }
    });

}

